package com.itboye.ihomebank.activity.myzhujia.qianbao;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.login.TimeCount;
import com.itboye.ihomebank.activity.me.ActivityZhangHu;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 添加银行卡(输入短信)
 * 
 */
public class ActivityAddBankCardTwo extends BaseOtherActivity implements
		Observer {
	View v_statusbar;
	TextView add_shap_title_tv;
	ImageView close_icon;

	TextView add_bank_send, add_bank02_ok;
	EditText add_bank_phone;
	EditText add_bank02_yzm;
	String phone;
	private UserPresenter userPresenter;
	private String uid;
	private String bank_no;
	private String bank_account;
	String avv,yxq,khh;
	String code;
	private String bank_cert;
	private String yzm;
	String idNumber;
	private TimeCount time;// 倒计时
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		case R.id.add_bank_send:// 发送验证码
			phone = add_bank_phone.getText().toString();
			if (phone.equals("")) {

				ByAlert.alert("请输入手机号码");
				return;
			}
			userPresenter.send(phone, "9");
			time.start();
			break;
		case R.id.add_bank02_ok:// 确认添加
			/**
			 * Parameters: uid 用户id bank_no 银行卡号 bank_account 是 银行卡绑定姓名
			 * bank_cert 银行绑定身份证号 bank_phone 银行卡绑定手机号 bank_cvv 否 信用卡后三位 bank_yxq
			 * 信用卡有效期
			 */
			yzm = add_bank02_yzm.getText().toString();
			if (yzm.equals("")) {
				ByAlert.alert("请输入验证码");
				return;
			}
			showProgressDialog("添加中,请稍后...",true);
			userPresenter.verification(phone, "9", yzm);

			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_add_bank_card02;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		setStatusBarHeight(v_statusbar);
		add_shap_title_tv.setText("添加银行卡");
		phone = SPUtils.get(this, null, SPContants.MOBILE, "") + "";
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		bank_no = getIntent().getStringExtra("bank_no");
		idNumber = getIntent().getStringExtra("idNumber");
		khh= getIntent().getStringExtra("khh");
		avv=getIntent().getStringExtra("avv");
		yxq=getIntent().getStringExtra("yxq");
		bank_account = SPUtils.get(this, null, SPContants.RealName, "") + "";
		bank_cert = SPUtils.get(this, null, SPContants.IdNumBer, "") + "";
		userPresenter = new UserPresenter(this);

		time = new TimeCount(60000, 1000, add_bank_send);// 构造CountDownTimer对象
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.send_success) {
				code = entity.getData().toString();
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.send_fail) {
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.bindwallet_success) {
				ActivityAddBankCard.getInstance().finish();
				ActivityZhangHu.getInstance().refresh();
				finish();
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.bindwallet_fail) {
				ByAlert.alert(entity.getData());
			} else if (entity.getEventType() == UserPresenter.verification_success) {
				userPresenter.bindBankCard(uid, bank_no, bank_account,
						idNumber, phone, avv, yxq,khh);
			} else if (entity.getEventType() == UserPresenter.verification_fail) {
				ByAlert.alert(entity.getData());
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
	}

}
